<html>
<head><meta charset="utf-8"><title>how to disallow ascription · t-lang/type-ascription · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/index.html">t-lang/type-ascription</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html">how to disallow ascription</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220681798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/220681798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#220681798">(Dec 22 2020 at 11:53)</a>:</h4>
<p>So in <a class="stream-topic" data-stream-id="213817" href="/#narrow/stream/213817-t-lang/topic/type.20ascription">#t-lang &gt; type ascription</a> we talked about disallowing type ascription in certain contexts...</p>



<a name="220681874"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/220681874" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#220681874">(Dec 22 2020 at 11:54)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/213817-t-lang/topic/type.20ascription/near/219352073">said</a>:</p>
<blockquote>
<p>There seem to be 3 proposals on the table:</p>
<ul>
<li>Change semantics of ascription to disallow coercion in lvalue contexts</li>
<li>Forbid ascription of "place expressions" altogether</li>
<li>Forbid ascription of "place expressions" altogether in lvalue contexts</li>
</ul>
<p>(I am going to adopt the term <em>lvalue context</em> to refer to the expression <code>E</code> in assignments <code>E = ...</code>, borrows (<code>&amp;E</code>, <code>&amp;mut E</code>), and auto-ref'd method calls <code>E.foo()</code>. <em>Place expression</em> refers to an expression that references an existing place, which is also one that could appear on the left-hand side of an assignment. Note that the other lvalue contexts (besides assignment) accept arbitrary expressions, but they introduce temporaries if the expression is not a place expression.)</p>
</blockquote>



<a name="220681878"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/220681878" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#220681878">(Dec 22 2020 at 11:54)</a>:</h4>
<p>I think we want that last bullet</p>



<a name="220681888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/220681888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#220681888">(Dec 22 2020 at 11:54)</a>:</h4>
<p>One way to do this would be to use the <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_typeck/expr_use_visitor/struct.ExprUseVisitor.html"><code>ExprUseVisitor</code></a></p>



<a name="220681933"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/220681933" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#220681933">(Dec 22 2020 at 11:55)</a>:</h4>
<p>this is some code that gives "callbacks" for various events</p>



<a name="220682019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/220682019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#220682019">(Dec 22 2020 at 11:56)</a>:</h4>
<p>in particular there is a delegate that gets callbacks. The <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_typeck/expr_use_visitor/trait.Delegate.html#tymethod.borrow"><code>borrow</code></a> callback is invoked when a given expression will be borrowed</p>



<a name="220682040"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/220682040" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#220682040">(Dec 22 2020 at 11:57)</a>:</h4>
<p>so e.g. if there is <code>&amp;x</code> or <code>x.foo()</code> (where <code>foo</code> is an <code>&amp;self</code> method) then it would be called on the expression <code>x</code></p>



<a name="220682101"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/220682101" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#220682101">(Dec 22 2020 at 11:58)</a>:</h4>
<p>I am imagining we could use this to look for cases where the expression <code>x</code> contains a type ascription, but it will take a bit of coding to do</p>



<a name="220708494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/220708494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#220708494">(Dec 22 2020 at 16:34)</a>:</h4>
<p>And then if an expression contains any type ascription, at any level, it can't be used as an lvalue? Which would include being passed as a mutable reference?</p>



<a name="220708745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/220708745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#220708745">(Dec 22 2020 at 16:36)</a>:</h4>
<p>What's the archetypal "how to cause problems with ascription in an lvalue context" example again?</p>



<a name="220714082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/220714082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#220714082">(Dec 22 2020 at 17:30)</a>:</h4>
<p>Its in second paragraph of description of <a href="https://github.com/rust-lang/rust/issues/78248#issue-727700204">rust#78248</a>, I think.</p>



<a name="220714116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/220714116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#220714116">(Dec 22 2020 at 17:30)</a>:</h4>
<blockquote>
<p>it would cause problems in contexts where references are being created, such as &amp;mut (x: T). This is because the reference that results would actually reference x, since x: T is defined to be a "place expression" just like x is. The problem then is that if the type of x and the type T are not the same, this permits unsoundness [...]</p>
</blockquote>



<a name="221797657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/221797657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#221797657">(Jan 06 2021 at 17:40)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> I'm not sure how to tell whether a method is an <code>&amp;self</code> method using <code>ExprUseVisitor</code>.  Is it guaranteed that every such method will have an adjustment for the receiver or can the adjustments for the receiver expression be empty on <code>&amp;self</code> methods?</p>



<a name="221927985"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/221927985" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#221927985">(Jan 07 2021 at 10:54)</a>:</h4>
<p>The compiler always re-borrows when the receiver is a reference, correct? So <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_typeck/expr_use_visitor/struct.ExprUseVisitor.html#method.walk_autoref"><code>walk_autoref</code></a> is always called on <code>&amp;self</code> methods right?</p>



<a name="221953587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/221953587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#221953587">(Jan 07 2021 at 15:16)</a>:</h4>
<p>Hey <span class="user-mention" data-user-id="328097">@BN</span> -- sorry for the delay</p>



<a name="221953621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/221953621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#221953621">(Jan 07 2021 at 15:16)</a>:</h4>
<p>Mmm it's not so much that we want to check for <code>&amp;self</code> methods <em>explicitly</em></p>



<a name="221953632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/221953632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#221953632">(Jan 07 2021 at 15:16)</a>:</h4>
<p>It's just that <code>ExprUseVisitor</code> will tell you where the borrows are</p>



<a name="221953650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/221953650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#221953650">(Jan 07 2021 at 15:16)</a>:</h4>
<p>Regardless of whether those are due to an <code>&amp;self</code> method or something else</p>



<a name="221964344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/221964344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#221964344">(Jan 07 2021 at 16:31)</a>:</h4>
<p>That's what I tried ininitially, i.e. I only tried to use the <code>borrow</code> method of delegate, but this also caused type ascriptions in contexts such as :</p>
<div class="codehilite"><pre><span></span><code>  let arr = [4,5,6];
  let p = &amp;arr;
  let fooer = Foo {a : p } ;

  fooer.foo(p : &amp;[u32])
</code></pre></div>
<p>to not be accepted, but this should compile, shouldn't it?</p>



<a name="221964376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/221964376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#221964376">(Jan 07 2021 at 16:31)</a>:</h4>
<p>I opened a WIP, can you take a look? <a href="https://github.com/rust-lang/rust/pull/80788">https://github.com/rust-lang/rust/pull/80788</a></p>



<a name="221964573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/221964573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#221964573">(Jan 07 2021 at 16:32)</a>:</h4>
<p>One more problem I encountered is that we can't use type ascriptions in <code>assert_eq</code> macros, which insert references to the arguments.</p>



<a name="222972111"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/222972111" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#222972111">(Jan 16 2021 at 09:45)</a>:</h4>
<p><span class="user-mention" data-user-id="328097">@BN</span> I'm going to take a look, apologies for disappearing :)</p>



<a name="222973010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/222973010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#222973010">(Jan 16 2021 at 10:07)</a>:</h4>
<p>No worries.</p>



<a name="222974434"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/222974434" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#222974434">(Jan 16 2021 at 10:44)</a>:</h4>
<p><span class="user-mention" data-user-id="328097">@BN</span>  OK I <a href="https://github.com/rust-lang/rust/pull/80788#pullrequestreview-569842790">left a review</a></p>



<a name="222974441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/222974441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#222974441">(Jan 16 2021 at 10:45)</a>:</h4>
<p>the main idea is in <a href="https://github.com/rust-lang/rust/pull/80788#discussion_r558835971">this set of comments</a>, I was thinking about the way to implement and I <em>think</em> the best thing would be to modify "place construction"</p>



<a name="222974446"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/222974446" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#222974446">(Jan 16 2021 at 10:45)</a>:</h4>
<p>I'm not sure how familiar you are with the code so let me know if that makes no sense to you</p>



<a name="223002516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223002516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223002516">(Jan 16 2021 at 19:56)</a>:</h4>
<p><span class="user-mention" data-user-id="328097">@BN</span>  btw <span class="user-mention" data-user-id="281950">@Aman Arora</span> is pretty familiar with this code and may be able to help</p>



<a name="223002523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223002523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223002523">(Jan 16 2021 at 19:56)</a>:</h4>
<p><span class="user-mention" data-user-id="281950">@Aman Arora</span> I can give more context</p>



<a name="223002530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223002530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223002530">(Jan 16 2021 at 19:57)</a>:</h4>
<p>that'd be nice <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span> I'll also try read through the thread</p>



<a name="223019650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223019650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223019650">(Jan 17 2021 at 02:35)</a>:</h4>
<p>Okay, so one of the issues that I see in the PR is that in <code>x: T</code> we are missing the <code>: T</code> in the span. This is because we <a href="https://github.com/rust-lang/rust/blob/a56bffb4f9d6f7791b2fa40412bd3e0549d76cc3/compiler/rustc_typeck/src/mem_categorization.rs#L353">only categorize</a> the expression to the left of <code>:</code>. </p>
<p>The reason we had introduced the <code>diag_expr_id</code> in ExprUseVisitor was to not modify the expression that is stored  <code>PlaceWithHirId</code>  (this expression that points to the actual place that was categorized) and rather pass back an expression that is better for reporting back to the user as part of the compiler diagnostics.  That said not sure if storing the complete expression is incorrect. </p>
<p>I'm not sure what HirId we want to be in <code>PlaceBase::Rvalue</code> and I know this suggestion came up before as well when we starting revamping hir::Place to be more precise. I can look it up again. But maybe utilizing <code>diag_expr_id</code> and then <code>tcx.hir().expect_expr(hir_id)</code> to get the expression in your part of the code is a better way than adding an <code>Option&lt;&amp;Expr&gt;</code>.</p>



<a name="223019815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223019815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223019815">(Jan 17 2021 at 02:41)</a>:</h4>
<p>Okay re-read the PR comment, Rvalue contains the expr id of the original expr that mem categorization was called on</p>



<a name="223019937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223019937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223019937">(Jan 17 2021 at 02:45)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281950">Aman Arora</span> <a href="#narrow/stream/269230-t-lang.2Ftype-ascription/topic/how.20to.20disallow.20ascription/near/223019815">said</a>:</p>
<blockquote>
<p>Okay re-read the PR comment, Rvalue contains the expr id of the original expr that mem categorization was called on</p>
</blockquote>
<p>and I looked up and this wsa basically what we had previously discussed as well to do. We didn't end up needing it, so this wasn't added.</p>



<a name="223024923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223024923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223024923">(Jan 17 2021 at 05:05)</a>:</h4>
<p>One thing that I forgot to say is that we want to make sure we don't break Clippy tests since it also uses ExprUseVisitor. </p>
<p>If you want to locally test changes you can do <code>./x.py test --stage 1 src/tools/clippy</code>.</p>



<a name="223026113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223026113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223026113">(Jan 17 2021 at 05:42)</a>:</h4>
<p>I should probably test this out but I think, I think this is how it will be</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="nc">U</span><span class="p">;</span><span class="w"></span>

<span class="n">f</span><span class="p">(</span><span class="w">  </span><span class="n">x</span>: <span class="nc">T</span><span class="w">  </span><span class="p">);</span><span class="w"></span>
<span class="c1">//   ^^^ Place `p` for the expression will have `p.ty() = U`</span>
</code></pre></div>
<p>And that is because it will categorize just the place for <code>x</code> and ignore the type ascription. This feels wrong <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="223129901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223129901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223129901">(Jan 18 2021 at 15:00)</a>:</h4>
<p><span class="user-mention" data-user-id="281950">@Aman Arora</span> Thanks for looking into this. If we treat type ascriptions as Rvalues  (presumable by calling <code>cat_rvalue</code> <a href="https://github.com/rust-lang/rust/blob/66eb9821666e0672a69a998d2331733c7a8996a5/compiler/rustc_typeck/src/mem_categorization.rs#L353">here</a>), wouldn't that lead to problems if the expression <code>e</code> of the type ascription <code>(e : T)</code> were an UpVar (and we wouldn't call <code>cat_expr</code> on <code>e</code>)?</p>



<a name="223175552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223175552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223175552">(Jan 19 2021 at 00:20)</a>:</h4>
<p>That's true. I think we probably want to process the expression itself like it's done today and then somehow wrap it and return it as if it were an rvalue</p>



<a name="223211995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223211995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223211995">(Jan 19 2021 at 11:03)</a>:</h4>
<p><span class="user-mention" data-user-id="281950">@Aman Arora</span> Ok thanks, but I'm not sure that we have to wrap the subexpression of type ascriptions. I've looked at the <code>memcategorization</code>code again and I think I have an idea about how we should handle this, if you see there anything wrong with my reasoning, please tell me. </p>
<p>Currently the way rvalues are handled in <code>memcategorization</code> is by just calling <code>cat_rvalue</code> (done <a href="https://github.com/rust-lang/rust/blob/7d7b22d78fa71cf18109dc9d186611f2ddf60d8e/compiler/rustc_typeck/src/mem_categorization.rs#L355-L382">here</a>), so even for cases such as tuples, in which the subexpressions could also contain UpVars we just ignore them here and classify the tuple as a whole as an rvalue. But <code>ExprUseVisitor</code> later walks the subexpressions of the expressions. So e.g. for  an expression consisting of a tuple we would <a href="https://github.com/rust-lang/rust/blob/7d7b22d78fa71cf18109dc9d186611f2ddf60d8e/compiler/rustc_typeck/src/expr_use_visitor.rs#L150-L156"><code>consume_expr</code></a>, then classify this tuple expression as an rvalue, delegate this using <code>delegate_consume</code> and then later walk the subexpressions, which may contain UpVars, using <code>walk_exprs</code>. If any of the subexpressions of the Tuple contain UpVars these will be classified correctly here.</p>
<p>For type ascriptions we will do the same. The reason why we treat type ascriptions differently was given in some comment pointing to <a href="https://rust-lang.github.io/rfcs/0803-type-ascription.html#type-ascription-and-temporaries">this part of the rfc</a>. Basically we currently classify a type ascription <code>(e : T)</code> as  an lvalue iff e is an lvalue.</p>



<a name="223265455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223265455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223265455">(Jan 19 2021 at 17:55)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> I'm still not sure whether I understand in which situations we want to forbid type ascriptions. Do we only want to forbid autorefs if they occur in callee positions? Or do we also want to forbid say autorefs that occur because of re-borrows for function arguments? Basically do we always want to forbid type ascriptions whenever delegate.borrow is called and some subexpression is a type ascription?</p>



<a name="223560105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223560105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223560105">(Jan 21 2021 at 19:39)</a>:</h4>
<p><span class="user-mention" data-user-id="328097">@BN</span> autoref only ever occurs in callee positio</p>



<a name="223560148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223560148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223560148">(Jan 21 2021 at 19:39)</a>:</h4>
<p>it refers to method calls like <code>a.foo()</code> where <code>foo</code> is an <code>&amp;self</code> method</p>



<a name="223560227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223560227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223560227">(Jan 21 2021 at 19:40)</a>:</h4>
<p><span class="user-mention silent" data-user-id="328097">BN</span> <a href="#narrow/stream/269230-t-lang.2Ftype-ascription/topic/how.20to.20disallow.20ascription/near/223265455">said</a>:</p>
<blockquote>
<p>Basically do we always want to forbid type ascriptions whenever delegate.borrow is called and some subexpression is a type ascription?</p>
</blockquote>
<p>yes</p>



<a name="223560240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223560240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223560240">(Jan 21 2021 at 19:40)</a>:</h4>
<p>I think that's correct</p>



<a name="223700352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223700352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223700352">(Jan 22 2021 at 21:07)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> Ok this is ready for another review. The test cases you suggested are in <code>src/test/ui/type-ascription/{as_ref,  borrow,  ref-def}</code>. I've also added some tests where automatic reborrows result in errors (for which I'm not sure whether these should yield errors) in <code>src/test/ui/type-ascription/automatic-reborrow</code> </p>
<p><a href="https://github.com/rust-lang/rust/pull/80788">https://github.com/rust-lang/rust/pull/80788</a></p>



<a name="223883347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/223883347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#223883347">(Jan 25 2021 at 11:23)</a>:</h4>
<p><span class="user-mention" data-user-id="328097">@BN</span> great!</p>



<a name="225407236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/225407236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#225407236">(Feb 06 2021 at 12:02)</a>:</h4>
<p><span class="user-mention" data-user-id="328097">@BN</span> sorry it took me a bit, I left a review, this is actually looking really close I think!</p>



<a name="225407240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/225407240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#225407240">(Feb 06 2021 at 12:02)</a>:</h4>
<p>I'm hoping to become more responsive now :) I've been working out various hold-ups in my schedule.</p>



<a name="226005584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/226005584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#226005584">(Feb 11 2021 at 15:18)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span>  Updated the PR, can you take another look, please?</p>



<a name="226052196"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269230-t-lang/type-ascription/topic/how%20to%20disallow%20ascription/near/226052196" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/269230-t-lang/type-ascription/topic/how.20to.20disallow.20ascription.html#226052196">(Feb 11 2021 at 20:23)</a>:</h4>
<p><span class="user-mention" data-user-id="328097">@BN</span> yep!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>